var datagridLine;
var datagridDevice;
var dialogLine;
var dialogDevice;
var dialogDeviceMove;
$(function () {

    datagridLine = $('#mdg_line').datagrid({
        method: "post",
        url: getRootPath() + '/fault/getLineList',
        fit: true,
        fitColumns: true,
        border: true,
        idField: 'line_id',
        striped: true,
        pagination: false,
        rownumbers: true,
        singleSelect: true,
        selectOnCheck: true,
        checkOnSelect: true,
        toolbar: '#tb_Line',
        columns: [[
            {field: 'ck', checkbox: true},
            {field: 'line_id', title: 'line_id', hidden: true},
            {field: 'workshop', title: '所属车间', sortable: false, width: 30},
            {
                field: 'line_name', title: '线别名称', sortable: false, width: 40,
                formatter: function (value, row, index) {
                    return "<span title='" + value + "'>" + value + "</span>";
                }
            },
            {field: 'remark', title: '备注', sortable: false, width: 30,
                formatter: function (value, row, index) {
                    return "<span title='" + value + "'>" + value + "</span>";
                }
            },
            {
                field: 'operate', title: '操作', width: 42,
                formatter: function (value, row, index) {
                    var d = "<a onclick='remove(" + row.line_id + ")' class='button-delete button-red'>删除</a>";
                    var e = "<a onclick='edit(" + index + ")' class='button-edit button-blue'>编辑</a>";
                    return e + '  ' + d;
                }
            }
        ]],
        onLoadSuccess:function(data) {
            $('.button-delete').linkbutton({});
            $('.button-edit').linkbutton({});
            IniDeviceDg();
        },
        onSelect: function (index, row) {
            $("#lineid").val(row.line_id);
            setVal('#device_name',"")
            $("#show_line_lable").text("以下为 "+row.line_name+" 的机器设备列表");
            queryDevice();
        },
        queryParams: {

        }
    });

});

//初始化设备列表
function IniDeviceDg() {
    var first_row_line_id=null;
    var rows = $("#mdg_line").datagrid("getRows");
    console.info(rows);
    setVal('#device_name',"");
    if (rows.length>0)
    {
        first_row_line_id = rows[0].line_id;
        $("#lineid").val(first_row_line_id);
        $("#show_line_lable").text("以下为 "+rows[0].line_name+" 的机器设备列表");
    }else
    {
        $("#lineid").val("");
        $("#show_line_lable").text("暂未选择线体");
    }

    datagridDevice = $('#mdg_device').datagrid({
        method: "post",
        url: getRootPath() + '/fault/getDeviceList',
        //fit: true,
        border: true,
        idField: 'device_id',
        striped: true,
        pagination: false,
        rownumbers: true,
        singleSelect: true,
        selectOnCheck: true,
        checkOnSelect: true,
        toolbar: '#tb_Device',
        columns: [[
            {field: 'ck', checkbox: true},
            {field: 'device_id', title: 'device_id', hidden: true},
            {field: 'device_name', title: '设备名称', sortable: false, width: 120},
            {field: 'device_num', title: '设备编号', sortable: false, width: 80},
            {field: 'remark', title: '备注', sortable: false, width: 80},
            {
                field: 'operate', title: '操作', width: 165,
                formatter: function (value, row, index) {
                    var d = "<a onclick='remove_device(" + row.device_id + ")' class='button-delete button-red'>删除</a>";
                    var e = "<a onclick='edit_device(" + index + ")' class='button-edit button-blue'>编辑</a>";
                    var m = "<a onclick='move_device(" + row.device_id + ")' class='button-edit button-green'>移动</a>";
                    return m+ '  '+e + '  ' + d;
                }
            }
        ]],
        onLoadSuccess:function(data) {
            $('.button-delete').linkbutton({});
            $('.button-edit').linkbutton({});
        },
        queryParams: {
            "line_id":first_row_line_id
        }
    });
}

function getFormJson(form, filterName, addName) {
    var o = {};
    var a = $(form).serializeArray();
    // addName是新增的键值对
    for (var i = 0; i < addName.length; i++) {
        a.push(addName[i]);
    }
    console.log(a);
    $.each(a, function () {
        console.log(this.name);
        if (o[this.name] !== undefined && this.name != filterName) {
            if (!o[this.name].push) {
                o[this.name] = [o[this.name]];
            }
            o[this.name].push(this.value || '');
        } else if (this.name != filterName) {
            o[this.name] = this.value || '';
        }
    });
    console.log(o);
    return o;
}

/**
 * 添加线体
 */
function add() {

    dialogLine = $("#dlg_line").dialog({
        title: '添加线体',
        width: 470,
        height: 250,
        maximizable: false,
        modal: true,
        buttons: [{
            text: '确认',
            iconCls: 'icon-ok',
            handler: function () {
                var isValid = $("#form_line").form('validate');

                var addObj = [];
                if (isValid) {
                    U.post({
                        url: getRootPath() + "/fault/addLine",
                        loading: true,
                        data: getFormJson("#form_line", "", addObj),//$('#form_main').serialize(),
                        success: function (data) {
                            if (data.code == 200) {
                                U.msg('添加成功');
                                dialogLine.dialog('close');
                                queryLine();
                            } else if (data.code == 400) {
                                U.msg('参数验证失败');
                            } else if (data.code == 409) {
                                U.msg('已存在');
                            } else {
                                U.msg('未知异常');
                            }
                        }
                    });
                }
            }
        }, {
            text: '取消',
            handler: function () {
                dialogLine.dialog('close');
            }
        }]
    });
    $("#form_line").form("clear");
}

/**
 * 编辑线体
 */
function edit(id) {

    var row;
    if (id == null) {//点击datagrid上面的修改
        row = datagridLine.datagrid('getSelected');
    } else {//点击datagrid对应行上的修改
        datagridLine.datagrid('selectRow', id);
        row = datagridLine.datagrid('getSelected');
    }

    if (row == null) {
        U.msg('请先选择线体');
        return;
    }

    dialogLine = $("#dlg_line").dialog({
        title: '编辑线体',
        width: 470,
        height: 250,
        maximizable: false,
        modal: true,
        buttons: [{
            text: '确认',
            iconCls: 'icon-ok',
            handler: function () {
                var isValid = $("#form_line").form('validate');
                var addObj = [{"name":"line_id","value":row.line_id}];
                if (isValid) {
                    U.post({
                        url: getRootPath() + "/fault/editLine",
                        loading: true,
                        data: getFormJson("#form_line", "", addObj),
                        success: function (data) {
                            if (data.code == 200) {
                                U.msg('修改成功');
                                dialogLine.dialog('close');
                                queryLine();
                            }else if (data.code == 400) {//参数验证失败
                                U.msg('参数验证失败');
                            }else if (data.code == 403) {
                                U.msg('非法请求');
                            }else if (data.code == 404) {
                                U.msg('未找到该分类');
                            } else if (data.code == 409) {
                                U.msg('该分类已存在');
                            } else {
                                U.msg('未知异常');
                            }
                        }
                    });
                }
            }
        }, {
            text: '取消',
            handler: function () {
                dialogLine.dialog('close');
            }
        }]
    });
    //setVal自定义函数 位置common.js 因为用了easyui-textbox 所以显示还需要特殊赋值
    setVal("#form_line_name", row.line_name);
    setVal("#workshop", row.workshop);
    setVal("#remark", row.remark);
}

/**
 * 删除线体
 */
function remove(id) {
    if (id == null) {
        var row = datagridLine.datagrid('getSelected');
        if (row == null) {
            U.msg('请先选择线体');
            return;
        } else {
            id = row.line_id;
        }
    }

    parent.$.messager.confirm('提示', '删除后此条线体下的机器设备也会删除且无法恢复您确定要删除？', function (data) {
        if (data) {
            U.get({
                url: getRootPath() + '/fault/deleLine?line_id=' + id,
                loading: true,
                success: function (data) {
                    if (data.code == 200) {
                        U.msg('删除成功');
                        queryDept();
                    } else if (data.code == 400) {//参数验证失败
                        U.msg('参数验证失败');
                    } else if (data.code == 403) {
                        U.msg('非法访问');
                    }else if (data.code == 408) {
                        U.msg(data.message);
                    }
                    else {
                        U.msg('未知异常');
                    }
                }
            });
        }
    });
}

/**
 * 添加线体设备
 */
function add_device() {

    if ($("#lineid").val()=="")
    {U.msg('请选择一条线体再添加设备！');return;}

    dialogDevice = $("#dlg_device").dialog({
        title: '添加线体设备',
        width: 460,
        height: 250,
        maximizable: false,
        modal: true,
        buttons: [{
            text: '确认',
            iconCls: 'icon-ok',
            handler: function () {
                var isValid = $("#form_device").form('validate');
                var lineid = $("#lineid").val();
                console.info("看看 "+lineid)
                var addObj = [{"name": "line_id", "value":lineid}];
                var obj = getFormJson("#form_device", "", addObj);
                obj.operator = username;
                if (isValid) {
                    U.post({
                        url: getRootPath() + "/fault/addDevice",
                        loading: true,
                        data: obj,//自定义过滤字段和追加字段
                        success: function (data) {
                            if (data.code == 200) {
                                U.msg('添加成功');
                                dialogDevice.dialog('close');
                                queryDevice();
                            } else if (data.code == 400) {
                                U.msg('参数验证失败');
                            } else if (data.code == 409) {
                                U.msg('设备已存在');
                            }else if (data.code == 408) {
                                U.msg(data.message);
                            }
                            else {
                                U.msg('未知异常');
                            }
                        }
                    });
                }
            }
        }, {
            text: '取消',
            handler: function () {
                dialogDevice.dialog('close');
            }
        }]
    });
    $("#form_device").form('clear');
}

function move_device(id) {
    dialogDeviceMove = $("#dlg_device_move").dialog({
        title: '设备转移',
        width: 300,
        height: 150,
        maximizable: false,
        modal: true,
        buttons: [{
            text: '确认',
            iconCls: 'icon-ok',
            handler: function () {
                var isValid = $("#form_device_move").form('validate');
                var addObj = [{"name": "device_id", "value": id}];
                var obj = getFormJson("#form_device_move", "", addObj);
                obj.operator = username;
                if (isValid) {
                    U.post({
                        url: getRootPath() + "/fault/moveDevice",
                        loading: true,
                        data: obj,
                        success: function (data) {
                            if (data.code == 200) {
                                U.msg('修改成功');
                                dialogDeviceMove.dialog('close');
                                queryDevice();
                            } else if (data.code == 400) {//参数验证失败
                                U.msg('参数验证失败');
                            } else if (data.code == 404) {
                                U.msg('未找到该分类');
                            } else if (data.code == 409) {
                                U.msg(data.message);
                            } else {
                                U.msg('未知异常');
                            }
                        }
                    });
                }
            }
        }, {
            text: '取消',
            handler: function () {
                dialogDeviceMove.dialog('close');
            }
        }]
    });
    $("#form_device_move").form('clear');
    //setVal自定义函数 位置common.js 因为用了easyui-textbox 所以显示还需要特殊赋值
    $.getJSON(getRootPath() + "/fault/getLineList", function (json) {
        $('#moveform_line_id').combobox({
            data: json.rows, //获取到的json 数据
            valueField: "line_id",
            textField: "line_name"
        });
        $("#moveform_line_id").combobox('setValue', $('#lineid').val());
    });
}

/**
 * 编辑设备
 */
function edit_device(id) {

    var row;
    if (id == null) {//点击datagrid上面的修改
        row = datagridDevice.datagrid('getSelected');
    } else {//点击datagrid对应行上的修改
        datagridDevice.datagrid('selectRow', id);
        row = datagridDevice.datagrid('getSelected');
    }

    if (row == null) {
        U.msg('请先选择设备');
        return;
    }

    dialogDevice = $("#dlg_device").dialog({
        title: '编辑设备',
        width: 460,
        height: 250,
        maximizable: false,
        modal: true,
        buttons: [{
            text: '确认',
            iconCls: 'icon-ok',
            handler: function () {
                var isValid = $("#form_device").form('validate');
                var addObj = [{"name": "device_id", "value": row.device_id}];
                var obj = getFormJson("#form_device", "", addObj);
                obj.operator = username;
                if (isValid) {
                    U.post({
                        url: getRootPath() + "/fault/editDevice",
                        loading: true,
                        data: obj,
                        success: function (data) {
                            if (data.code == 200) {
                                U.msg('修改成功');
                                dialogDevice.dialog('close');
                                queryDevice();
                            } else if (data.code == 400) {//参数验证失败
                                U.msg('参数验证失败');
                            } else if (data.code == 404) {
                                U.msg('未找到该分类');
                            } else if (data.code == 408) {
                                U.msg(data.message);
                            } else {
                                U.msg('未知异常');
                            }
                        }
                    });
                }
            }
        }, {
            text: '取消',
            handler: function () {
                dialogDevice.dialog('close');
            }
        }]
    });
    $("#form_device").form('clear');
    //setVal自定义函数 位置common.js 因为用了easyui-textbox 所以显示还需要特殊赋值
    setVal("#form_device_name", row.device_name);
    setVal("#form_device_num", row.device_num);
    setVal("#remark2", row.remark);
}

/**
 * 删除设备
 */
function remove_device(id) {
    if (id == null) {
        var row = datagridExceptionType.datagrid('getSelected');
        if (row == null) {
            U.msg('请先选择设备');
            return;
        } else {
            id = row.id;
        }
    }

    parent.$.messager.confirm('提示', '删除后无法恢复您确定要删除？', function (data) {
        if (data) {
            U.get({
                url: getRootPath() + '/fault/deleDevice?device_id=' + id,
                loading: true,
                success: function (data) {
                    if (data.code == 200) {
                        U.msg('删除成功');
                        queryDevice();
                    } else if (data.code == 400) {//参数验证失败
                        U.msg('参数验证失败');
                    } else if (data.code == 404) {
                        U.msg('未找到该设备');
                    } else if (data.code == 408) {
                        U.msg(data.message);
                    }else {
                        U.msg('未知异常');
                    }
                }
            });
        }
    });
}

function queryDevice() {
    $('#mdg_device').datagrid('load', {
            "line_id":$("#lineid").val(),
            "device_name": $("#device_name").val()
        }
    );
}

function queryLine() {
    $('#mdg_line').datagrid('load', {
            "line_name": $("#line_name").val()
        }
    );
}
